#include <all_wlib.h>
#pragma hdrstop

#include "nv_Main.h"

//---------------------------------------------------------------------------
bool __fastcall TMainForm::idSplitFile( const UrlInfo& it )
  {  CONSTSTR  m;
     char      fnm[ MAX_PATH_SIZE ];

     static const char *Months[12] = { "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" };

     if ( SplitInterval == 1 )  m = Message( "%s-%02d-%04d", Months[it.Time.mon-1], it.Time.mday, it.Time.year ); else
     if ( SplitInterval >= 30 ) m = Message( "%s-%04d", Months[it.Time.mon-1], it.Time.year );
      else {
       int num = (it.Time.mday / SplitInterval) * SplitInterval;
       m = Message( "%s-%02d_%02d-%04d",
                    Months[it.Time.mon-1],
                    Max( 1, num ), num + SplitInterval-1,
                    it.Time.year );
     }

     switch( GOPT( "SplitFormat", (int)sfSquid ) ) {
       case     sfSquid: SNprintf( fnm, sizeof(fnm), "access.%s.log", m ); break;
       case sfNaviscope: SNprintf( fnm, sizeof(fnm), "browse-%s.txt", m ); break;
     }

     PFileInfo p = Files.Find( fnm );
     if (!p) {
       p = Files.Add( new FileInfo( SplitPath, fnm ) );
       if ( !p->File && SplitAskFile )
         switch( AskYesNoCancel( Handle, NULL,
                                 "Can not create file \"%s\"\n"
                                 "Error: \"%s\"\n"
                                 "\n"
                                 "What you wana do?\n"
                                 "\n"
                                 "YES: Ignore current file\n"
                                 "NO: Ignore all create file errors\n"
                                 "CANCEL: Abort split procedure\n",
                                 p->FileName, FIO_ERROR ) ) {
           case IDYES:
                    break;

           case IDNO: SplitAskFile = false;
                    break;

           case IDCANCEL: return false;
         }
     }

     if ( p->File )
       switch( GOPT( "SplitFormat", (int)sfSquid ) ) {
         case     sfSquid: fprintf( p->File,
                                    "%d.0 0 %s TCP_MISS/000 %I64u GET %s/%s - DIRECT/- -\n",
                                    it.Time.GetTT(NULL), it.Host.Text(), it.Size, it.Site.Text(), it.Url.Text() );
                       break;

         case sfNaviscope: fprintf( p->File,
                                    "\"%s/%s\",%s,%I64u,\"Fri %s %02d %02d:%02d:%02d %4d\"\n",
                                    it.Site.Text(), it.Url.Text(), it.Host.Text(), it.Size,
                                    Months[it.Time.mon-1],it.Time.mday,it.Time.hour,it.Time.min,it.Time.sec,it.Time.year );
                       break;
     }

 return true;
}
